/*
 * Copyright (c) 2016, 2024, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

package jdk.test.lib.jittester;

import jdk.test.lib.jittester.visitors.ByteCodeVisitor;

import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.function.Function;

/**
 * Generates class files from IRTree
 */
class ByteCodeGenerator extends TestsGenerator {
    private static final String DEFAULT_SUFFIX = "bytecode_tests";

    ByteCodeGenerator() {
        super(DEFAULT_SUFFIX, s -> new String[0], "-Xcomp");
    }

    ByteCodeGenerator(String suffix, Function<String, String[]> preRunActions, String jtDriverOptions) {
        super(suffix, preRunActions, jtDriverOptions);
    }

    @Override
    public void accept(IRTreeGenerator.Test test) {
        IRNode mainClass = test.mainClass();
        generateClassFiles(mainClass, test.privateClasses());
        generateSeparateJtregHeader(test.seed(), mainClass);
        compilePrinter();
        generateGoldenOut(mainClass.getName());
    }

    private void generateSeparateJtregHeader(long seed, IRNode mainClass) {
        String mainClassName = mainClass.getName();
        writeFile(generatorDir, mainClassName + ".java", getJtregHeader(mainClassName, seed));
    }

    private void generateClassFiles(IRNode mainClass, IRNode privateClasses) {
        String mainClassName = mainClass.getName();
        ensureExisting(generatorDir);
        try {
            ByteCodeVisitor vis = new ByteCodeVisitor();
            if (privateClasses != null) {
                privateClasses.accept(vis);
            }
            mainClass.accept(vis);
            writeFile(mainClassName + ".class", vis.getByteCode(mainClassName));
            if (privateClasses != null) {
                privateClasses.getChildren().forEach(c -> {
                    String name = c.getName();
                    writeFile(name + ".class", vis.getByteCode(name));
                });
            }
        } catch (Throwable t) {
            Path errFile = generatorDir.resolve(mainClassName + ".err");
            try (PrintWriter pw = new PrintWriter(Files.newOutputStream(errFile,
                    StandardOpenOption.CREATE, StandardOpenOption.WRITE))) {
                t.printStackTrace(pw);
            } catch (IOException e) {
                t.printStackTrace();
                throw new Error("can't write error to error file " + errFile, e);
            }
        }
    }

    private void writeFile(String fileName, byte[] bytecode) {
        try {
            Files.write(generatorDir.resolve(fileName), bytecode);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public static void main(String[] args) throws Exception {
        ProductionParams.initializeFromCmdline(args);
        IRTreeGenerator.initializeWithProductionParams();

        ByteCodeGenerator generator = new ByteCodeGenerator();

        for (String mainClass : ProductionParams.mainClassNames.value()) {
            var test = IRTreeGenerator.generateIRTree(mainClass);
            generator.generateClassFiles(test.mainClass(), test.privateClasses());
            generator.generateSeparateJtregHeader(test.seed(), test.mainClass());
        }
    }
}
